﻿namespace OleDbDAL.Systems
{
    using DBUtility;
    using IDal.Systems;
    using Model.Systems;
    using System;
    using System.Data;
    using System.Text;

    public class DeptApp : IDept
    {
        private string dbname = "Systems";

        public void Add(DeptEnt model)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("insert into T_Dept(");
            builder.Append("ChainNum,DeptName,DeptMan,Remark,IsUse");
            builder.Append(")");
            builder.Append(" values (");
            builder.Append("" + model.ChainNum + ",");
            builder.Append("'" + model.DeptName + "',");
            builder.Append("'" + model.DeptMan + "',");
            builder.Append("'" + model.Remark + "',");
            builder.Append("" + model.IsUse + "");
            builder.Append(")");
            DbHelperOledb.ExecuteSql(this.dbname, builder.ToString());
        }

        public void Delete(int DeptId)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("delete from T_Dept ");
            builder.Append(" where DeptId=" + DeptId);
            DbHelperOledb.ExecuteSql(this.dbname, builder.ToString());
        }

        public DataTable GetList(string strWhere)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("select * from T_Dept ");
            if (strWhere.Trim() != "")
            {
                builder.Append(" where " + strWhere);
            }
            builder.Append(" order by DeptId ");
            return DbHelperOledb.Query(this.dbname, builder.ToString());
        }

        public int GetMaxId(string swhere)
        {
            return DbHelperOledb.GetMaxID(this.dbname, "DeptId", "T_Dept", swhere);
        }

        public DeptEnt GetModel(int DeptId)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("select DeptId,ChainNum,DeptName,DeptMan,Remark,IsUse from T_Dept ");
            builder.Append(" where DeptId=" + DeptId);
            DeptEnt ent = new DeptEnt();
            DataTable table = DbHelperOledb.Query(this.dbname, builder.ToString());
            ent.DeptId = DeptId;
            if (table.Rows.Count > 0)
            {
                if (table.Rows[0]["ChainNum"].ToString() != "")
                {
                    ent.ChainNum = int.Parse(table.Rows[0]["ChainNum"].ToString());
                }
                ent.DeptName = table.Rows[0]["DeptName"].ToString();
                ent.DeptMan = table.Rows[0]["DeptMan"].ToString();
                ent.Remark = table.Rows[0]["Remark"].ToString();
                if (table.Rows[0]["IsUse"].ToString() != "")
                {
                    ent.IsUse = int.Parse(table.Rows[0]["IsUse"].ToString());
                }
                return ent;
            }
            return null;
        }

        public DataTable GetPageList(int PageSize, int PageIndex, string strWhere)
        {
            string orderString = "";
            int pageCount = 0;
            int recordCount = 0;
            StringBuilder builder = new StringBuilder();
            builder.Append("select * from T_Dept ");
            return DbHelperOledb.ExecutePager(this.dbname, PageIndex, PageSize, "", builder.ToString(), strWhere, orderString, out pageCount, out recordCount, "DeptId");
        }

        public void Update(DeptEnt model)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("update T_Dept set ");
            builder.Append("ChainNum=" + model.ChainNum + ",");
            builder.Append("DeptName='" + model.DeptName + "',");
            builder.Append("DeptMan='" + model.DeptMan + "',");
            builder.Append("Remark='" + model.Remark + "',");
            builder.Append("IsUse=" + model.IsUse + "");
            builder.Append(" where DeptId=" + model.DeptId + "");
            DbHelperOledb.ExecuteSql(this.dbname, builder.ToString());
        }
    }
}

